package im.agents.migration;


/*import im.agents.IMAgent;
import jade.content.ContentElement;
import jade.content.lang.Codec.CodecException;
import jade.content.lang.sl.SLCodec;
import jade.content.onto.OntologyException;
import jade.content.onto.UngroundedException;
import jade.content.onto.basic.Action;
import jade.content.onto.basic.Result;
import jade.core.AID;*/
import jade.core.Agent;
import jade.core.Location;
import jade.core.behaviours.Behaviour;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
/*import jade.domain.JADEAgentManagement.JADEManagementOntology;
import jade.domain.JADEAgentManagement.QueryAgentsOnLocation;
import jade.domain.JADEAgentManagement.QueryPlatformLocationsAction;
import jade.domain.mobility.MobilityOntology;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.util.leap.Iterator;*/

/**
 * 
 * @author Silvia
 * ELIMINARE QUESTA CLASSE
 *
 */
public class Migration extends Behaviour {
	
	public Migration(Agent myAgent){
		super(myAgent);
	}

	@Override
	public void action() {
		DFAgentDescription[] ds = findMigrationManager();
		if(ds== null||ds.length<=0){
			System.out.println("MigrationManager not found");
		}else {
			//MigrationManager mm = searchMigrationManagerFromAID(ds[0].getName());
			//if(mm!=null){
			System.out.println("Faccio la richiesta di locazione migliore per la migrazione");
			//BestLocationRequester bestLocationRequester = new BestLocationRequester(myAgent);
			//myAgent.addBehaviour(bestLocationRequester);
		}
	}
		/*	while(bestLocationRequester.getBestLocation()==null){
				myAgent.doWait(100);
			}
				Location location = bestLocationRequester.getBestLocation();
				if (location!=null){
					myAgent.doMove(location);
				}
				else 
					System.out.println("Impossibile trovare una locazione in cui migrare");
		}/*
		
	}

	/*private MigrationManager searchMigrationManagerFromAID(AID name) {
		
		myAgent.getContentManager().registerLanguage(new SLCodec());
		myAgent.getContentManager().registerOntology(MobilityOntology.getInstance());

		// Get available locations with AMS
		sendRequest(new Action(myAgent.getAMS(), new QueryPlatformLocationsAction()));

		//Receive response from AMS
	    MessageTemplate mt = MessageTemplate.and(
				                  MessageTemplate.MatchSender(myAgent.getAMS()),
				                  MessageTemplate.MatchPerformative(ACLMessage.INFORM));
	         ACLMessage resp = myAgent.blockingReceive(mt);
	         ContentElement ce = null;
			try {
				ce = myAgent.getContentManager().extractContent(resp);
			} catch (UngroundedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (CodecException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (OntologyException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	         Result result = (Result) ce;
	         jade.util.leap.Iterator it = result.getItems().iterator();
	         while (it.hasNext()) {
	            Location container = (Location)it.next();
	            Iterator agents = getAgentInLocation(container);
	            while(agents.hasNext()){
	            	IMAgent agent = (IMAgent)agents.next();
	            	if(agent.getAID()==name)
	            		return (MigrationManager)agent;
	            }
			 }
		 
		// TODO Auto-generated method stub
		return null;
	}*/

/*	private Iterator getAgentInLocation(Location container){
		SLCodec codec = new SLCodec();
		ACLMessage req = new ACLMessage(ACLMessage.REQUEST);
		req.setSender(myAgent.getAID());
		req.addReceiver(myAgent.getAMS());
		req.setLanguage(codec.getName());
		req.setOntology(JADEManagementOntology.getInstance().getName());
		QueryAgentsOnLocation qal = new QueryAgentsOnLocation();
		qal.setLocation(container);
		Action action = new Action(myAgent.getAMS(), qal);
		try {
			myAgent.getContentManager().fillContent(req,action);
		} catch (CodecException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (OntologyException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		myAgent.send(req);
		ACLMessage resp = myAgent.blockingReceive();
		ContentElement ce=null;
		try {
			ce = myAgent.getContentManager().extractContent(resp);
		} catch (UngroundedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (CodecException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OntologyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Result myresult = (Result) ce;
		Iterator agentsinlocation = myresult.getItems().iterator();
		
		return agentsinlocation;
		
	}*/

	private DFAgentDescription[] findMigrationManager() {
		DFAgentDescription[] ds = null;
		try {
			DFAgentDescription dfd = new DFAgentDescription();
			ServiceDescription s = new ServiceDescription ();
			s.setName("migration-manager");
			s.setType("migration-manager");
			dfd.addServices(s);
			ds = DFService.search(myAgent, dfd);
			
		} catch (FIPAException e) {
			e.printStackTrace();
			return null;
		}
		return ds;
	}
	
	/*void sendRequest(Action action) {
		ACLMessage request = new ACLMessage(ACLMessage.REQUEST);
		request.setLanguage(new SLCodec().getName());
		request.setOntology(MobilityOntology.getInstance().getName());
		try {
			myAgent.getContentManager().fillContent(request, action);
			request.addReceiver(action.getActor());
			myAgent.send(request);
		}
		catch (Exception ex) { ex.printStackTrace(); }
	}*/

	@Override
	public boolean done() {
		// TODO Auto-generated method stub
		return false;
	}

}
